package lyl.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBsql {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		testOracle();
	}
	
	public static void testSqlserver(){
		String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
		String dbURL = "jdbc:microsoft:sqlserver://192.168.1.250:1433;DatabaseName=test1";
		// String dbURL = "jdbc:jtds
		// qlserver://localhost:1433/pubs;tds=8.0;lastupdatecount=true;autoReconnect=true;SelectMethod=Cursor";

		String userName = "cmspro4t";
		String userPwd = "cmspro4t";
		try {
			Class.forName(driverName);
			Connection dbConn = DriverManager.getConnection(dbURL, userName,
					userPwd);
			System.out.println("Connection Successful!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void testOracle() {
		Connection conn = getConnection("cmspro","cmspro");
		String sql = "CREATE TABLE TEST1  (ID  VARCHAR2(100) NOT NULL,NAME VARCHAR2(255)  NOT NULL)";
		try {
			Statement sm = conn.createStatement();
			sm.executeUpdate(sql);
			sql = "insert into test1(id,name) values('爱爱爱','爱爱爱')";
			sm.executeUpdate(sql);
			sql = "select * from test1";
			ResultSet rs = sm.executeQuery(sql);
			while(rs.next()){
				System.out.println(rs.getString(1));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	private static Connection getConnection(String user, String password) {
		Connection con = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
			con = DriverManager.getConnection(
					"jdbc:oracle:thin:@192.168.1.246:1521:cmspro", user,
					password);
			System.out.println("Connection Successful!");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return con;
	}
}
